using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using DTO;
using BUS;
using Utility;
public partial class admin_A_ChiTietDonHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Loaddata();
            Loadddl();
        }
    }
    private void Loadddl()
    {
        TinhTrang_DonHangCollection tinhtrangdonhang = TinhTrang_DonHang_BUS.GetAll();

        hpl_tinhtrang.DataTextField = TinhTrang_DonHangColumns.TenTinhTrang.ToString();
        hpl_tinhtrang.DataValueField = TinhTrang_DonHangColumns.MaTinhTrang_DH.ToString();
        hpl_tinhtrang.DataSource = tinhtrangdonhang;

        hpl_tinhtrang.DataBind();
    }
    public void Loaddata()
    {
        
        Criteria cri = new Criteria();
        int total = 0;
        ptrangchitietdonhang.PageSize = 3;
        int id = int.Parse(Request.QueryString["id"]);
        HoaDon_DTO hoadon = HoaDon_BUS.GetByID(id);
        lit_mahd.Text = hoadon.MaHD.ToString();
        //NhanVien_DTO nhanvien = NHANVIEN_BUS.GetByID(hoadon.MaNV);
        //lit_manhanvien.Text = nhanvien.TenNV;
        KhachHang_DTO khachhang = KHACHHANG_BUS.GetByID(hoadon.MaKH);
        lit_makhachhang.Text = khachhang.HoTen;
        HinhThucThanhToan_DTO htt = HinhThucThanhToan_BUS.GetByID(hoadon.MaHinhThuc_ThanhToan);
        lit_mahinhthuctt.Text = htt.TenHinhThucThanhToan;

        hpl_tinhtrang.SelectedValue = hoadon.MaTinhTrang_DH.ToString();

        //TinhTrang_DonHang_DTO tinhtrang_donhang = TinhTrang_DonHang_BUS.GetByID(hoadon.MaTinhTrang_DH);

        //hpl_tinhtrang.SelectedValue = tinhtrang_donhang.TenTinhTrang;
        //hpl_tinhtrang.NavigateUrl = "~/admin/A_CapNhatDonHang.aspx?mahd=" + hoadon.MaHD;
        lit_ngaydathang.Text = hoadon.NgayDatHang.ToString();
        lit_ngaygiaohang.Text = hoadon.NgayGiaoHang.ToString();
        //lit_ngaygiohuy.Text = hoadon.NgayGioHuy_DonHang.ToString();
        lit_diachigiao.Text = hoadon.DiaChiGiao;
        lit_tongtien.Text = hoadon.TongTien.ToString();

        cri.add(Condition.AND, tbCTHoaDonColumns.MaHD, Condition.EQUAL, id);
        tbCTHoaDonCollection cthoadon = CTHoaDon_BUS.Get(cri.Criter, 1, 1, tbHinhAnhColumns.MaMH.ToString(), OrderDirection.ASC.ToString(), out total);
        //HinhAnh_DTO hinhclt = HinhAnh_BUS.GetByID(id);
        Repeater1.DataSource = cthoadon;
        Repeater1.DataBind();
        ptrangchitietdonhang.ItemCount = total;
        if (total <= ptrangchitietdonhang.PageSize)
        {
            ptrangchitietdonhang.Visible = false;
        }
        else
        {
            ptrangchitietdonhang.Visible = true;
        }


    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
           e.Item.ItemType == ListItemType.Item)
        {
            CTHoaDon_DTO cthoadon = (CTHoaDon_DTO)e.Item.DataItem;
            Literal lit_TenSP = (Literal)e.Item.FindControl("lit_TenSP");
            MatHang_DTO sanPham = MATHANG_BUS.GetByID(cthoadon.MaMH);
            lit_TenSP.Text = sanPham.TenMH;
            Literal lit_ThanhTien = (Literal)e.Item.FindControl("lit_ThanhTien");
            double thanhtien = (cthoadon.SoLuong * cthoadon.DonGia);
            lit_ThanhTien.Text = thanhtien.ToString();
        }

    }
    protected void ptrangchitietdonhang_Command(object sender, CommandEventArgs e)
    {
        ptrangchitietdonhang.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        Loaddata();

    }
    protected void btn_capnhat_Click(object sender, EventArgs e)
    {
        //try
        //{
        int id = int.Parse(Request.QueryString["id"].ToString());

        HoaDon_DTO hd = HoaDon_BUS.GetByID(id);
        hd.MaHD = int.Parse(lit_mahd.Text);
        //hd.MaNV = int.Parse(lit_manhanvien.Text);
        //hd.MaKH = int.Parse(lit_makhachhang.Text);
        //hd.MaHinhThuc_ThanhToan = int.Parse(lit_mahinhthuctt.Text);
        hd.MaTinhTrang_DH = int.Parse(hpl_tinhtrang.SelectedValue);
        //hd.NgayDatHang = DateTime.Parse(lit_ngaydathang.Text);
        //hd.NgayGiaoHang =DateTime.Parse(lit_ngaygiaohang.Text);
        //hd.NgayGioHuy_DonHang = DateTime.Parse(lit_ngaygiohuy.Text);
        //hd.DiaChiGiao = lit_diachigiao.Text;
        //hd.TongTien = float.Parse(lit_tongtien.Text);

        HoaDon_BUS.Update(hd);

        //}

        //catch
        //{
        //    //    Response.Redirect("~/admin/LoaiHang.aspx");
        //}
        //Response.Redirect(Request.RawUrl);
    }
}
